home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-11-30 | 4.8 KB | 231 lines | [TEXT/KAHL] |
- /***********************************************************************************
- CFamily.h
-
- Copyright © 1994 B-Ray Software. All rights reserved.
- Developed using Symantec C++ 7.0.2 and Symantec's TCL library.
- Portions of this code courtesy Symantec, Inc.
-
- This code may be freely distributed as long as this notice remains. This code
- may not be used in any commercial software without the consent of B-Ray Software.
-
- ---
-
- CFamily class methods. The CFamily class describes a relationship between
- children and a parent. Although it is similar to the CCollaborator class, the
- CFamily class is meant to be used as a base for Pane management, and I did not
- want multiple CCollaborator-based classes floating around my derived CPane's.
- Take a look at CExpanderPane to see what I mean.
-
- ***********************************************************************************/
-
- #include "CFamily.h"
-
- typedef struct {
- long message; // message identifier
- void *info; // any information a child wants to convey
- } FamilyInfo;
-
-
- TCL_DEFINE_CLASS_M0( CFamily );
-
-
- /*
- * Constructor
- *
- * Just initializes variables.
- */
-
- CFamily :: CFamily()
- {
- itsParent = NULL;
- itsChildren = NULL;
- }
-
-
- /*
- * Destructor
- *
- * Just zaps out instance variables. Since classes derived from us will use multiple
- * inheritance and a class from TCL, we don't want to zap anything on our end. Just
- * hope that TCL cleans itself up properly.
- */
-
- CFamily :: ~CFamily()
- {
- TCLForgetObject( itsChildren );
- itsParent = NULL;
- }
-
-
- /*
- * InsertChildAt method
- *
- * Adds a child to our list of children in the family. If a list of children does not
- * exist, we create one.
- */
-
- void CFamily :: InsertChildAt( CFamily *aChild, long index )
- {
- if ( itsChildren == NULL ) {
- itsChildren = TCL_NEW( CFamilyList, () );
- }
-
- aChild->SetParent( this );
- itsChildren->InsertAt( aChild, index );
- }
-
-
- /*
- * RemoveChildAt method
- *
- * Removes the child at the given index.
- */
-
- void CFamily :: RemoveChildAt( long index )
- {
- if ( itsChildren )
- itsChildren->DeleteItem( index );
- }
-
-
- /*
- * ResolveToLeaf method
- *
- * Walks the family tree until it hits a leaf node. The beginOrEnd parameter indicates
- * whether we always keap to the left (begin) or right (end) of our list of children nodes.
- * Specifying anything else is really meaningless.
- */
-
- CFamily *CFamily :: ResolveToLeaf( Boolean beginOrEnd )
- {
- CFamily *aChild = NULL;
-
- if ( itsChildren ) {
- aChild = beginOrEnd ? LastChild() : FirstChild(); // get child of interest
- if ( aChild ) {
- aChild = aChild->ResolveToLeaf( beginOrEnd ); // and resolve it
- }
- }
-
- if ( aChild == NULL ) {
- aChild = this; // return ourselves if no children are available
- }
-
- return aChild;
- }
-
-
- /*
- * TellChild method
- *
- * Given a message indicator and some information, this routine will tell all of the children
- * the message and info. Works much like the BroadcastChange() method for CCollaborator.
- */
-
- static void TellEachChild( CFamily *aChild, long param )
- {
- FamilyInfo *info = (FamilyInfo *)param;
- aChild->ParentMessage( info->message, info->info );
- }
-
- void CFamily :: TellChildren( long message, void *param )
- {
- FamilyInfo info;
-
- if ( itsChildren ) {
- info.message = message;
- info.info = param;
- itsChildren->DoForEach1( TellEachChild, (long )&info );
- }
- }
-
-
- /*
- * TellParent method
- *
- * Simply tells the parent what the message was.
- */
-
- void CFamily :: TellParent( long message, void *param )
- {
- if ( itsParent ) {
- itsParent->ChildMessage( this, message, param );
- }
- }
-
-
- /*
- * ChildMessage method
- *
- * Received a message from a child. Just pass it on to our parent. We don't respond to
- * specific messages.
- */
-
- void CFamily :: ChildMessage( CFamily *aChild, long message, void *param )
- {
- TellParent( message, param );
- }
-
-
- /*
- * ParentMessage method
- *
- * Received a message from our parent. Just pass it on to our children. We don't respond
- * to specific messages.
- */
-
- void CFamily :: ParentMessage( long message, void *param )
- {
- TellChildren( message, param );
- }
-
-
- /*
- * PutTo method
- *
- * Writes to the stream all of the info we need to save.
- */
-
- void CFamily :: PutTo( CStream &stream )
- {
- long loop, numChildren = GetNumberChildren();
- CFamily *aChild;
-
- stream << numChildren;
-
- for ( loop = 1; loop <= numChildren; ++loop ) {
- aChild = itsChildren->NthItem( loop );
- PutObject( stream, aChild );
- }
-
- aChild = itsParent;
- PutObject( stream, aChild );
- }
-
-
- /*
- * GetFrom method
- *
- * Reads from the stream all of the info that we saved.
- */
-
- void CFamily :: GetFrom( CStream &stream )
- {
- long loop, numChildren;
- CFamily *aChild;
- CPane *aPane;
-
- stream >> numChildren;
- if ( numChildren > 0 ) {
- itsChildren = TCL_NEW( CFamilyList, () );
- for ( loop = 1; loop <= numChildren; ++loop ) {
- GetObject( stream, aChild );
- TCL_ASSERT( aChild != NULL );
- itsChildren->InsertAt( aChild, loop );
- }
- }
-
- GetObject( stream, aChild );
- itsParent = aChild;
- }
-